// const debounce = {
//   mounted(el, binding) {
//     if (typeof binding.value !== 'function')
//       return
 
//     let timer = null
//     el.__handleClick__ = function () {
//       if (timer)
//         clearInterval(timer)
 
//       timer = setTimeout(() => {
//         binding.value()
//       }, 500)
//     }
//     el.addEventListener('click', el.__handleClick__)
//   },
//   beforeUnmount(el) {
//     el.removeEventListener('click', el.__handleClick__)
//   },
// }
// export default debounce


 
function debounce(fn, delay) {
  let timeoutId = null;
  return function (...args) {
    if (timeoutId !== null) {
      clearTimeout(timeoutId);
    }
    timeoutId = setTimeout(() => {
      fn(...args);
    }, delay);
  };
}
 
const vDebounce = {
  beforeMount(el, binding) {
    if (typeof binding.value !== 'function') {
      throw new Error('Directive value must be a function');
    }
    const delay = binding.arg ? parseInt(binding.arg, 10) : 300; // 默认延迟300ms
    el['onVDebounceClick'] = debounce(binding.value, delay);
    el.addEventListener('click', el['onVDebounceClick']);
  },
  unmounted(el) {
    el.removeEventListener('click', el['onVDebounceClick']);
    el['onVDebounceClick'] = null;
  },
};
 
export default vDebounce;